package com.mayjean.gateway.config;

import com.mayjean.gateway.listener.DynamicGatewayRouteDefinitionListener;
import com.mayjean.gateway.props.NezhaGatewayProperties;
import com.mayjean.nacos.operator.NacosOperator;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.config.GatewayAutoConfiguration;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * gateway configuration
 *
 * @author kyleju
 */
@Configuration
@EnableConfigurationProperties({NezhaGatewayProperties.class})
@AutoConfigureBefore({GatewayAutoConfiguration.class, ErrorWebFluxAutoConfiguration.class})
@RequiredArgsConstructor
public class GatewayConfiguration {

	@Bean
	@ConditionalOnBean(NacosOperator.class)
	@ConditionalOnProperty(value = "nezha.gateway.dynamic", havingValue = "true")
	public RouteDefinitionRepository dynamicGatewayPropertiesListener() {
		return new DynamicGatewayRouteDefinitionListener();
	}
}
